﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace P2P_Backup
{
    public partial class frmSettings : Form
    {
        public frmSettings()
        {
            InitializeComponent();
        }    
        
        private void frmSettings_Load(object sender, EventArgs e)
        {
            cmbSize.SelectedIndex = 0;
            if (System.IO.File.Exists("P2PBackup.xml"))
            {
                txtPassword.Text = P2PBackup.Instance.ArchiveKey;
                txtConfirm.Text = P2PBackup.Instance.ArchiveKey;
                txtFolder.Text = P2PBackup.Instance.ArchivePath;
                long len = P2PBackup.Instance.MaxArchiveSize;
                int i = 0;
                while (len >= 1024)
                {
                    i++;
                    len = len / 1024;
                }
                nmSize.Value = (Decimal) len;
                cmbSize.SelectedIndex = i - 1;

            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.ShowDialog();
            if (fbd.SelectedPath != string.Empty)
            {
                txtFolder.Text = fbd.SelectedPath;
            } 
        }

        private void btnSubmit_Click_1(object sender, EventArgs e)
        {
            if (txtConfirm.Text == txtPassword.Text)
            {
                long maxSize = (long)nmSize.Value;
                switch (cmbSize.SelectedItem.ToString())
                {
                    case "MB":
                        maxSize = maxSize * 1024;
                        break;
                    case "GB":
                        maxSize = maxSize * 1024 * 1024;
                        break;
                    case "TB":
                        maxSize = maxSize * 1024 * 1024 * 1024;
                        break;
                }
                P2PBackup.Instance.ArchiveKey = txtPassword.Text;
                P2PBackup.Instance.ArchivePath = txtFolder.Text;
                P2PBackup.Instance.MaxArchiveSize = maxSize;
                P2PBackup.Instance.Save();
                this.Close();
            }
            else
            {
                MessageBox.Show("Please make sure your password is correct.", "Check Password!");
                txtConfirm.Text = "";
                txtPassword.Text = "";
                txtPassword.Focus();
            }
        }


    }
}
